package com.application.moneycalculator.db;

import com.application.moneycalculator.log.Log_;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseOpenHelper extends SQLiteOpenHelper {
	
	private final String TAG = "DataBaseOpenHelper";

    private static final int DATABASE_VERSION = 2;
    private static final String DATABASE_NAME = "MoneyCalculator.db";
    public  static final String SETTINGS_TABLE_NAME = "settings";
    
    private static final String SETTINGS_TABLE_CREATE =
                "CREATE TABLE " + SETTINGS_TABLE_NAME + " (" + 
                				MoneyCalculator.Settings._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + 
                				MoneyCalculator.Settings.SETTINGS_TABLE_FIELD_SETTING		+ " INTEGER," +
                				MoneyCalculator.Settings.SETTINGS_TABLE_FIELD_SETTING_VALUE + " INTEGER" +
                										");";
    
public  static final String EXPENSES_TABLE_NAME = "expenses";
    
    private static final String EXPENSES_TABLE_CREATE =
                "CREATE TABLE " + EXPENSES_TABLE_NAME + " (" + 
                				MoneyCalculator.Expense._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + 
                				MoneyCalculator.Expense.EXPENSES_TABLE_FIELD_EXPENSE_DATE		+ " STRING," +
                				MoneyCalculator.Expense.EXPENSES_TABLE_FIELD_EXPENSE_AMOUNT		+ " STRING," +
                				MoneyCalculator.Expense.EXPENSES_TABLE_FIELD_EXPENSE_CURRENCY	+ " STRING" +
                										");";

    DataBaseOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

	@Override
	public void onCreate(SQLiteDatabase db) {
		Log_.out(TAG, "onCreate");
		db.execSQL(SETTINGS_TABLE_CREATE);
		db.execSQL(EXPENSES_TABLE_CREATE);
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log_.out(TAG, "Upgrading database from version " + oldVersion + " to "
        + newVersion + ", which will destroy all old data");
		
        db.execSQL("DROP TABLE IF EXISTS " + SETTINGS_TABLE_NAME);
        
        db.execSQL("DROP TABLE IF EXISTS " + EXPENSES_TABLE_NAME);
        
        onCreate(db);
	}
}